package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.enumeration.YesNoEnum;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.business.model.HospitalConsult;
import com.yunfeisoft.business.service.inter.HospitalConsultService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: HospitalConsultController
 * Description: 入院咨询Controller
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@RestController
public class HospitalConsultController extends BaseController {

    @Autowired
    private HospitalConsultService hospitalConsultService;

    /**
     * 添加入院咨询
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/hospitalConsult/save", method = RequestMethod.POST)
    public Response save(HospitalConsult record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "consultName", "咨询者为空");
        validator.required(request, "relationship", "与入院者关系为空");
        validator.required(request, "elderName", "入院者为空");
        validator.required(request, "sex", "性别为空");
        validator.required(request, "age", "年龄为空");
        validator.required(request, "household", "户籍为空");
        validator.required(request, "address", "现居住地址为空");
        validator.required(request, "phone", "联系电话为空");
        validator.required(request, "health", "身体状况为空");
        validator.required(request, "remark", "入院要求为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        User user = ApiUtils.getLoginUser();
        record.setOrgId(user.getOrgId());

        hospitalConsultService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改入院咨询
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/hospitalConsult/modify", method = RequestMethod.POST)
    public Response modify(HospitalConsult record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "consultName", "咨询者为空");
        validator.required(request, "relationship", "与入院者关系为空");
        validator.required(request, "elderName", "入院者为空");
        validator.required(request, "sex", "性别为空");
        validator.required(request, "age", "年龄为空");
        validator.required(request, "household", "户籍为空");
        validator.required(request, "address", "现居住地址为空");
        validator.required(request, "phone", "联系电话为空");
        validator.required(request, "health", "身体状况为空");
        validator.required(request, "remark", "入院要求为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        hospitalConsultService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询入院咨询
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/hospitalConsult/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        HospitalConsult record = hospitalConsultService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询入院咨询
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/hospitalConsult/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String consultName = ServletRequestUtils.getStringParameter(request, "consultName", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("consultName", consultName);

        User user = ApiUtils.getLoginUser();
        if (user.getIsSys() == YesNoEnum.NO_CANCEL.getValue()) {
            params.put("orgId", user.getOrgId());
        }
        Page<HospitalConsult> page = hospitalConsultService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除入院咨询
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/hospitalConsult/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        hospitalConsultService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
